<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the attach method from the JanusClient class, for the Dart programming language.">
  <title>attach method - JanusClient class - janus_client library - Dart API</title>

  
  <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="../../static-assets/github.css">
  <link rel="stylesheet" href="../../static-assets/styles.css">
  <link rel="icon" href="../../static-assets/favicon.png">

</head>

<body data-base-href="../../"
      data-using-base-href="false">

<div id="overlay-under-drawer"></div>

<header id="title">
  <button id="sidenav-left-toggle" type="button">&nbsp;</button>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">janus_client</a></li>
    <li><a href="../../janus_client/janus_client-library.html">janus_client</a></li>
    <li><a href="../../janus_client/JanusClient-class.html">JanusClient</a></li>
    <li class="self-crumb">attach method</li>
  </ol>
  <div class="self-name">attach</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<main>

  <div id="dartdoc-sidebar-left" class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
      <form class="search-sidebar" role="search">
        <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
      </form>
    </header>
    
    <ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
      <li><a href="../../index.html">janus_client</a></li>
      <li><a href="../../janus_client/janus_client-library.html">janus_client</a></li>
      <li><a href="../../janus_client/JanusClient-class.html">JanusClient</a></li>
      <li class="self-crumb">attach method</li>
    </ol>
    
    <h5>JanusClient class</h5>
    <ol>
    
        <li class="section-title"><a href="../../janus_client/JanusClient-class.html#constructors">Constructors</a></li>
        <li><a href="../../janus_client/JanusClient/JanusClient.html">JanusClient</a></li>
    
        <li class="section-title">
            <a href="../../janus_client/JanusClient-class.html#instance-properties">Properties</a>
        </li>
        <li><a href="../../janus_client/JanusClient/apiSecret.html">apiSecret</a></li>
        <li><a href="../../janus_client/JanusClient/currentJanusURI.html">currentJanusURI</a></li>
        <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/hashCode.html">hashCode</a></li>
        <li><a href="../../janus_client/JanusClient/iceServers.html">iceServers</a></li>
        <li><a href="../../janus_client/JanusClient/isConnected.html">isConnected</a></li>
        <li><a href="../../janus_client/JanusClient/isUnifiedPlan.html">isUnifiedPlan</a></li>
        <li><a href="../../janus_client/JanusClient/isUnifiedPlan_.html">isUnifiedPlan_</a></li>
        <li><a href="../../janus_client/JanusClient/maxEvent.html">maxEvent</a></li>
        <li><a href="../../janus_client/JanusClient/refreshInterval.html">refreshInterval</a></li>
        <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/runtimeType.html">runtimeType</a></li>
        <li><a href="../../janus_client/JanusClient/server.html">server</a></li>
        <li><a href="../../janus_client/JanusClient/sessionId.html">sessionId</a></li>
        <li><a href="../../janus_client/JanusClient/token.html">token</a></li>
        <li><a href="../../janus_client/JanusClient/withCredentials.html">withCredentials</a></li>
    
        <li class="section-title"><a href="../../janus_client/JanusClient-class.html#instance-methods">Methods</a></li>
        <li><a href="../../janus_client/JanusClient/attach.html">attach</a></li>
        <li><a href="../../janus_client/JanusClient/connect.html">connect</a></li>
        <li><a href="../../janus_client/JanusClient/destroy.html">destroy</a></li>
        <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
        <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/toString.html">toString</a></li>
    
        <li class="section-title inherited"><a href="../../janus_client/JanusClient-class.html#operators">Operators</a></li>
        <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/operator_equals.html">operator ==</a></li>
    
    
    
    
    
    </ol>
  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-main-content" class="col-xs-12 col-sm-9 col-md-8 main-content">
      <div><h1><span class="kind-method">attach</span> method </h1></div>

    <section class="multi-line-signature">
      <span class="returntype">Future</span>
            <span class="name ">attach</span>
(<wbr><ol class="parameter-list"><li><span class="parameter" id="attach-param-plugin"><span class="type-annotation"><a href="../../Plugin/Plugin-class.html">Plugin</a></span> <span class="parameter-name">plugin</span></span></li>
</ol>)
      
    </section>
    <section class="desc markdown">
      <p>Attach Plugin to janus instance, for any project you need single janus instance to which you can attach any number of supported plugin</p>
    </section>
    
    <section class="summary source-code" id="source">
      <h2><span>Implementation</span></h2>
      <pre class="language-dart"><code class="language-dart">attach(Plugin plugin) async {
  var transaction = _uuid.v4();
  Map&lt;String, dynamic&gt; request = {
    &quot;janus&quot;: &quot;attach&quot;,
    &quot;plugin&quot;: plugin.plugin,
    &quot;transaction&quot;: transaction
  };
  request[&quot;token&quot;] = token;
  request[&quot;apisecret&quot;] = apiSecret;
  request[&quot;session_id&quot;] = sessionId;
  plugin.context = this;
  Map&lt;String, dynamic&gt; configuration = {
    &quot;iceServers&quot;: iceServers.map((e) =&gt; e.toMap()).toList()
  };
  if(isUnifiedPlan){
    configuration.putIfAbsent(&#39;sdpSemantics&#39;, () =&gt; &#39;unified-plan&#39;);
  }
  RTCPeerConnection peerConnection =
      await createPeerConnection(configuration, {});
  WebRTCHandle webRTCHandle = WebRTCHandle(
    iceServers: iceServers,
  );
  webRTCHandle.pc = peerConnection;
  plugin.webRTCHandle = webRTCHandle;
  plugin.apiSecret = apiSecret;
  plugin.sessionId = _sessionId;
  plugin.token = token;
  plugin.pluginHandles = _pluginHandles;
  plugin.transactions = _transactions;

  if (!isUnifiedPlan) {
    if (plugin.onLocalStream != null) {
      plugin.onLocalStream(peerConnection.getLocalStreams());
    }

    peerConnection.onAddStream = (MediaStream stream) {
      if (plugin.onRemoteStream != null) {
        plugin.onRemoteStream(stream);
      }
    };
  } else {
    peerConnection.onTrack = (RTCTrackEvent event) async {
      if (event.streams == null || event.transceiver == null) return;
      var mid =
          event.transceiver != null ? event.transceiver.mid : event.track.id;
      plugin.onRemoteTrack(event.streams[0], event.track, mid, true);
      if (event.track.onEnded == null) return;
      event.track.onEnded = () async {
        if (webRTCHandle.remoteStream != null) {
          webRTCHandle.remoteStream.removeTrack(event.track);
          var mid = event.track.id;
          var transceiver = peerConnection.transceivers
              .firstWhere((element) =&gt; element.receiver.track == event.track);
          mid = transceiver.mid;
          plugin.onRemoteTrack(event.streams[0], event.track, mid, false);
        }
      };
      event.track.onMute = () async {
        if (webRTCHandle.remoteStream != null) {
          webRTCHandle.remoteStream.removeTrack(event.track);
          var mid = event.track.id;
          var transceiver = peerConnection.transceivers
              .firstWhere((element) =&gt; element.receiver.track == event.track);
          mid = transceiver.mid;
          plugin.onRemoteTrack(event.streams[0], event.track, mid, false);
        }
      };
    };
  }
  &#47;&#47;      send trickle
  peerConnection.onIceCandidate = (RTCIceCandidate candidate) async {
    debugPrint(&#39;sending trickle&#39;);
    Map&lt;dynamic, dynamic&gt; request = {
      &quot;janus&quot;: &quot;trickle&quot;,
      &quot;candidate&quot;: candidate.toMap(),
      &quot;transaction&quot;: &quot;sendtrickle&quot;
    };
    request[&quot;session_id&quot;] = plugin.sessionId;
    request[&quot;handle_id&quot;] = plugin.handleId;
    request[&quot;apisecret&quot;] = plugin.apiSecret;
    request[&quot;token&quot;] = plugin.token;

    &#47;&#47;checking and posting using websocket if in available
    if (!_usingRest) {
      plugin.webSocketSink.add(stringify(request));
    } else {
      &#47;&#47;posting using rest mechanism
      var data = await _postRestClient(request, handleId: plugin.handleId);
      print(&quot;trickle sent&quot;);
      print(data);
    }
  };

  &#47;&#47;WebSocket Related Code
  if (_webSocketSink != null &amp;&amp;
      _webSocketStream != null &amp;&amp;
      _webSocketChannel != null) {
    var opaqueId = plugin.opaqueId;
    if (plugin.opaqueId != null) request[&quot;opaque_id&quot;] = opaqueId;
    _webSocketSink.add(stringify(request));
    print(&#39;error here&#39;);
    _transactions[transaction] = (data) {
      if (data[&quot;janus&quot;] != &quot;success&quot;) {
        plugin.onError(
            &quot;Ooops: &quot; + data[&quot;error&quot;].code + &quot; &quot; + data[&quot;error&quot;][&quot;reason&quot;]);
        return null;
      }
      print(&#39;attaching plugin success&#39;);
      print(data);
      int handleId = data[&quot;data&quot;][&quot;id&quot;];
      debugPrint(&quot;Created handle: &quot; + handleId.toString());
      &#47;&#47;&#47; attaching websocket sink and stream on plugin handle
      plugin.webSocketStream = _webSocketStream;
      plugin.webSocketSink = _webSocketSink;
      plugin.handleId = handleId;
      _pluginHandles[handleId] = plugin;
      debugPrint(_pluginHandles.toString());
      if (plugin.onSuccess != null) {
        plugin.onSuccess(plugin);
      }
    };
    _webSocketStream.listen((event) {
      print(&#39;outer event&#39;);
      print(event);
      if (parse(event)[&quot;transaction&quot;] == transaction) {
        print(&#39;got event&#39;);
        print(event);
        _transactions[transaction](parse(event));
      }
    });

    _webSocketStream.listen((event) {
      _handleEvent(plugin, parse(event));
    });
  } else {
    &#47;&#47;attaching plugin considering rest as fallback mechanism
    var data = await _postRestClient(request);
    if (data[&quot;janus&quot;] != &quot;success&quot;) {
      debugPrint(&quot;Ooops: &quot; +
          data[&quot;error&quot;][&quot;code&quot;].toString() +
          &quot; &quot; +
          data[&quot;error&quot;][&quot;reason&quot;]);
      plugin.onError(&quot;Error: &quot; +
          data[&quot;error&quot;][&quot;code&quot;].toString() +
          &quot; &quot; +
          data[&quot;error&quot;][&quot;reason&quot;]);
      return null;
    }
    int handleId = data[&quot;data&quot;][&quot;id&quot;];
    plugin.handleId = handleId;
    debugPrint(&quot;Created handle: &quot; + handleId.toString());

    &#47;&#47;attaching event handler using http polling
    _eventHandler(plugin);
    &#47;&#47;adding plugin handle to plugin handles map
    _pluginHandles[handleId] = plugin;
    &#47;&#47;if not provided then don&#39;t attempt callback
    if (plugin.onSuccess != null) {
      plugin.onSuccess(plugin);
    }
  }
}</code></pre>
    </section>

  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-right" class="col-xs-6 col-sm-6 col-md-2 sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    janus_client
      0.0.4
  </span>

</footer>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="../../static-assets/typeahead.bundle.min.js"></script>
<script src="../../static-assets/highlight.pack.js"></script>
<script src="../../static-assets/URI.js"></script>
<script src="../../static-assets/script.js"></script>


</body>

</html>
